<?php
/**
 * @purpose 微信模板消息.
 * User: peter
 * Date: 2018/7/20
 * Time: 上午9:55
 */

namespace extend;


use think\facade\Cache;
use think\facade\Log;

class WeChatTemplateMessage
{
    const API  = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=';

    /**
     * @purpose 提成入账提醒
     * @param array $data = [
     *  'openid' => '',
     *  'quota' => '',
     *  'level' => '',
     *  'time' => '',
     *  'nickname' => '',
     *  'order_no' => '',
     * ]
     * @param array $data
     * @return bool
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public static function income (array $data): bool
    {
        $template_id                = 'E_jGi2OsZotYhHPUfvnF9BUy_Z_bm022ydemvRSo_Ws';
        $access_token               = Functions::getAccessToken();
        $api                        = self::API.$access_token;
        $options                    = [];
        $options['touser']          = $data['openid'];
        $options['template_id']     = $template_id;
        $options['url']             = rtrim(config('client_domain'),'/').'/profit';
        $options['data']            = [];
        $options['data']['first']   = [
            'value' => '盟友确认收货,提成已入账',
            'color' => '#173177'
        ];
        $options['data']['keyword1']   = [
            'value' => $data['quota'].'元',
            'color' => '#173177'
        ];
        $options['data']['keyword2']   = [
            'value' => $data['level'].'盟友交易完成',
            'color' => '#173177'
        ];
        $options['data']['keyword3']   = [
            'value' => $data['time'],
            'color' => '#173177'
        ];
        $options['data']['keyword4']   = [
            'value' => $data['nickname'],
            'color' => '#173177'
        ];
        $options['data']['keyword5']   = [
            'value' => $data['order_no'],
            'color' => '#173177'
        ];
        $options['data']['remark']   = [
            'value' => '点击查看详情',
            'color' => '#173177'
        ];
        $result     = \Requests::post($api, [], json_encode($options));
        $result     = json_decode($result->body, true);
        Log::write('income_wechat_message_notice: '.json_encode($result),'info');
        return ((int) $result['errcode'] === 0);
    }

    /**
     * @purpose 订单发货通知
     * @param array $data = [
     *  'openid' => '',
     *  'goods_title' => '',
     *  'time' => '',
     *  'express_company' => '',
     *  'express_no' => '',
     *  'address' => '',
     * ]
     * @param array $data
     * @return bool
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public static function deliverGoods (array $data): bool
    {
        $template_id                = 'zNNTexMZjDzpimMlm2lCHHj3p9vW6I_BYe1990ZR51Y';
        $access_token               = Functions::getAccessToken();
        $api                        = self::API.$access_token;
        $options                    = [];
        $options['touser']          = $data['openid'];
        $options['template_id']     = $template_id;
        $options['url']             = rtrim(config('client_domain'),'/').'/order/3';
        $options['data']            = [];
        $options['data']['first']   = [
            'value' => 'Hi，亲爱哒：您在V奢商城购买的商品已经发货啦,正快马加鞭向您飞奔而去',
            'color' => '#173177'
        ];
        $options['data']['keyword1']   = [
            'value' => $data['goods_title'],
            'color' => '#173177'
        ];
        $options['data']['keyword2']   = [
            'value' => $data['time'],
            'color' => '#173177'
        ];
        $options['data']['keyword3']   = [
            'value' => $data['express_company'],
            'color' => '#173177'
        ];
        $options['data']['keyword4']   = [
            'value' => $data['express_no'],
            'color' => '#173177'
        ];
        $options['data']['keyword5']   = [
            'value' => $data['address'],
            'color' => '#173177'
        ];
        $options['data']['remark']   = [
            'value' => '点击查看详情',
            'color' => '#173177'
        ];
        $result     = \Requests::post($api, [], json_encode($options));
        $result     = json_decode($result->body, true);
        Log::write('deliver_goods_wechat_message: '.json_encode($result),'info');
        return ((int) $result['errcode'] === 0);
    }

    /**
     * @purpose 会员下单支付成功时通知父级
     * @param array $data = [
     *  'openid' => '',
     *  'nickname' => 'Peter',
     *  'level' => '一级|二级',
     *  'quota' => '100.00元',
     *  'bonus' => '15.00元',
     *  'create_date' => ''
     *  'expect_income_date' => '2017年7月20 12:12:12',
     * ]
     * @param array $data
     * @return bool
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public static function consumption (array $data): bool
    {
        $template_id                = 'SbX-X2D3hY6dVV-epyhm_aaI-4ySPjHRRIxuZ-WhtW8';
        $access_token               = Functions::getAccessToken();
        $api                        = self::API.$access_token;
        $options                    = [];
        $options['touser']          = $data['openid'];
        $options['template_id']     = $template_id;
        $options['url']             = rtrim(config('client_domain'),'/').'/profit';
        $options['data']            = [];
        $options['data']['first']   = [
            'value' => $data['level'].'盟友消费通知!',
            'color' => '#173177'
        ];
        $options['data']['keyword1']   = [
            'value' => $data['nickname'],
            'color' => '#173177'
        ];
        $options['data']['keyword2']   = [
            'value' => '总金额: '. $data['quota']. '元, 提成: '.$data['bonus'].'元(待入账)',
            'color' => '#173177'
        ];
        $options['data']['keyword3']   = [
            'value' => $data['create_date'],
            'color' => '#173177'
        ];
        $options['data']['remark']   = [
            'value' => '预计入账时间: '. $data['expect_income_date']. ', 点击查看!',
            'color' => '#173177'
        ];
        $result     = \Requests::post($api, [], json_encode($options));
        $result     = json_decode($result->body, true);
        Log::write('consumption_wechat_message_notice: '.json_encode($result),'info');
        return ((int) $result['errcode'] === 0);
    }

    /**
     * @purpose 用户注册时发送邀请注册成功通知
     * @param array $data = [
     *  'openid' => '',
     *  'level'  => '一级|二级',
     *  'nickname'  => 'peter',
     *  'create_date'  => 'peter',
     *  'first'  => 45,
     *  'second'  => 11,
     * ]
     * @param array $data
     * @return bool
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public static function invitationRegister (array $data): bool
    {
        $template_id                = 'roJVCmtHoTyCChgOqxtNoegaUUwwH1eRiRgveN5yhTY';
        $access_token               = Functions::getAccessToken();
        $api                        = self::API.$access_token;
        $options                    = [];
        $options['touser']          = $data['openid'];
        $options['template_id']     = $template_id;
        $options['url']             = rtrim(config('client_domain'),'/').'/team';
        $options['data']            = [];

        $options['data']['first']   = [
            'value' => '恭喜您, 有新的'.$data['level'].'盟友加入您的团队!',
            'color' => '#173177'
        ];
        $options['data']['keyword1']   = [
            'value' => $data['nickname'],
            'color' => '#173177'
        ];
        $options['data']['keyword2']   = [
            'value' => $data['nickname'],
            'color' => '#173177'
        ];
        $options['data']['keyword3']   = [
            'value' => $data['create_date'],
            'color' => '#173177'
        ];
        $options['data']['remark']   = [
            'value' => '您目前有一级盟友'.$data['first'].'位,二级盟友'. $data['second'].'位, 点击查看',
            'color' => '#173177'
        ];
        $result     = \Requests::post($api, [], json_encode($options));
        $result     = json_decode($result->body, true);
        Log::write('invitation_register_wechat_notice: '.json_encode($result),'info');
        return ((int) $result['errcode'] === 0);
    }
}